load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
load("@rules_python//python:py_binary.bzl", "py_binary")

# Python binary for timings code generation
py_binary(
    name = "timings",
    srcs = ["timings.py"],
    main = "timings.py",
)

# Timings code generation rules
genrule(
    name = "timings_lp384_cc",
    srcs = ["//icefuzz:timings_lp384.txt"],
    outs = ["timings-lp384.cc"],
    cmd = "ICESTORM_TIMINGS_TEMPLATE=$(execpath //icefuzz:timings_lp384.txt) $(execpath :timings) lp384 > $@",
    cmd_bat = "set ICESTORM_TIMINGS_TEMPLATE=$(execpath //icefuzz:timings_lp384.txt) && $(execpath :timings) lp384 > $@",
    tools = [":timings"],
)

genrule(
    name = "timings_lp1k_cc",
    srcs = ["//icefuzz:timings_lp1k.txt"],
    outs = ["timings-lp1k.cc"],
    cmd = "ICESTORM_TIMINGS_TEMPLATE=$(execpath //icefuzz:timings_lp1k.txt) $(execpath :timings) lp1k > $@",
    cmd_bat = "set ICESTORM_TIMINGS_TEMPLATE=$(execpath //icefuzz:timings_lp1k.txt) && $(execpath :timings) lp1k > $@",
    tools = [":timings"],
)

genrule(
    name = "timings_lp8k_cc",
    srcs = ["//icefuzz:timings_lp8k.txt"],
    outs = ["timings-lp8k.cc"],
    cmd = "ICESTORM_TIMINGS_TEMPLATE=$(execpath //icefuzz:timings_lp8k.txt) $(execpath :timings) lp8k > $@",
    cmd_bat = "set ICESTORM_TIMINGS_TEMPLATE=$(execpath //icefuzz:timings_lp8k.txt) && $(execpath :timings) lp8k > $@",
    tools = [":timings"],
)

genrule(
    name = "timings_hx1k_cc",
    srcs = ["//icefuzz:timings_hx1k.txt"],
    outs = ["timings-hx1k.cc"],
    cmd = "ICESTORM_TIMINGS_TEMPLATE=$(execpath //icefuzz:timings_hx1k.txt) $(execpath :timings) hx1k > $@",
    cmd_bat = "set ICESTORM_TIMINGS_TEMPLATE=$(execpath //icefuzz:timings_hx1k.txt) && $(execpath :timings) hx1k > $@",
    tools = [":timings"],
)

genrule(
    name = "timings_hx8k_cc",
    srcs = ["//icefuzz:timings_hx8k.txt"],
    outs = ["timings-hx8k.cc"],
    cmd = "ICESTORM_TIMINGS_TEMPLATE=$(execpath //icefuzz:timings_hx8k.txt) $(execpath :timings) hx8k > $@",
    cmd_bat = "set ICESTORM_TIMINGS_TEMPLATE=$(execpath //icefuzz:timings_hx8k.txt) && $(execpath :timings) hx8k > $@",
    tools = [":timings"],
)

genrule(
    name = "timings_up5k_cc",
    srcs = ["//icefuzz:timings_up5k.txt"],
    outs = ["timings-up5k.cc"],
    cmd = "ICESTORM_TIMINGS_TEMPLATE=$(execpath //icefuzz:timings_up5k.txt) $(execpath :timings) up5k > $@",
    cmd_bat = "set ICESTORM_TIMINGS_TEMPLATE=$(execpath //icefuzz:timings_up5k.txt) && $(execpath :timings) up5k > $@",
    tools = [":timings"],
)

genrule(
    name = "timings_u4k_cc",
    srcs = ["//icefuzz:timings_u4k.txt"],
    outs = ["timings-u4k.cc"],
    cmd = "ICESTORM_TIMINGS_TEMPLATE=$(execpath //icefuzz:timings_u4k.txt) $(execpath :timings) u4k > $@",
    cmd_bat = "set ICESTORM_TIMINGS_TEMPLATE=$(execpath //icefuzz:timings_u4k.txt) && $(execpath :timings) u4k > $@",
    tools = [":timings"],
)

# icetime - Timing analysis tool
cc_binary(
    name = "icetime",
    srcs = [
        "icetime.cc",
        "iceutil.cc",
        ":timings_hx1k_cc",
        ":timings_hx8k_cc",
        ":timings_lp1k_cc",
        ":timings_lp384_cc",
        ":timings_lp8k_cc",
        ":timings_u4k_cc",
        ":timings_up5k_cc",
    ],
    copts = [
        "-std=c++11",
    ],
    data = [
        "//icefuzz:timing_data",
    ],
    defines = [
        "PREFIX=\\\"/usr/local\\\"",
        "CHIPDB_SUBDIR=\\\"icebox\\\"",
    ],
    linkopts = ["-lm"],
    visibility = ["//visibility:public"],
)
